package com.netbao.controller;

import com.netbao.annotation.SysLog;
import com.netbao.sys.model.Role;
import com.netbao.sys.model.User;
import com.netbao.sys.service.RoleService;
import com.netbao.sys.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @ResponseBody
    @RequestMapping("/dologin")
    @SysLog
    public Map<String,Object> login(String imageCode, @Valid User user, BindingResult bindingResult, HttpSession session){
        return userService.login(imageCode,user,bindingResult,session);
    }


    @ResponseBody
    @RequestMapping("/saveRole")
    public Map<String,Object> saveRole(Integer roleId,boolean flag,HttpSession session)throws Exception{
        List<Role> roleList = (List<Role>)session.getAttribute("roleList");
        if(flag){
            Role currentRole=roleList.get(0);
            session.setAttribute("currentRole", currentRole);
        }else {
            for (Role role : roleList) {
                if(role.getId().equals(roleId)){
                    session.setAttribute("currentRole", role);
                }
            }
        }
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("success", true);
        return map;
    }
}
